package com.novellotus.iov.carvice.model;

import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.adapter.CarFragmentAdapter;
import com.novellotus.iov.carvice.bean.MqttVehicleBean;
import com.novellotus.iov.carvice.bean.RxCarListBean;
import com.novellotus.iov.carvice.fragment.BaseCarFragment;
import com.novellotus.iov.carvice.fragment.CarStatuesFragment;
import com.novellotus.iov.carvice.fragment.CarViewpagerFragment;
import com.novellotus.iov.carvice.utils.MyUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Rich on 2017/7/27.
 */

public class CarViewPagerModel {

    private BaseCarFragment.CarFragmentInterface carFragmentInterface;
    private CarViewpagerFragment context;
    private ImageView[] imageViews;
    private int currentPosition;
    private List<BaseCarFragment> fragmentList = new ArrayList<>();

    private CarFragmentAdapter carFragmentAdapter;
    private CarViewpagerFragment.CarSwitchInterface justInterface;
    private int point;
    private int distance;
    private int chooseWidth;
    private int chooseHeight;

    public CarViewPagerModel(CarViewpagerFragment context, CarFragmentAdapter carFragmentAdapter, CarViewpagerFragment.CarSwitchInterface justInterface, BaseCarFragment.CarFragmentInterface carFragmentInterface) {
        this.context = context;
        this.carFragmentAdapter = carFragmentAdapter;
        this.justInterface = justInterface;
        this.carFragmentInterface = carFragmentInterface;
        point = (int) context.getResources().getDimension(R.dimen.point);
        distance = (int) context.getResources().getDimension(R.dimen.point_margin);
        chooseWidth = (int) context.getResources().getDimension(R.dimen.point_choose_width);
        chooseHeight = (int) context.getResources().getDimension(R.dimen.point_choose_height);

    }


    /**
     * @param fragmentManager
     * @param viewPager
     * @param recordsBeanList 设置fragment
     */
    public void setFragmentList(FragmentManager fragmentManager, ViewPager viewPager, List<RxCarListBean.RecordsBean> recordsBeanList) {
        carFragmentAdapter = new CarFragmentAdapter(fragmentManager, getFragments(recordsBeanList));
        viewPager.setAdapter(carFragmentAdapter);
    }

    /**
     * @param recordsBeanList 刷新数据
     */
    public void updateFragmentList(List<RxCarListBean.RecordsBean> recordsBeanList) {
        fragmentList.clear();
        carFragmentAdapter.setFragments(getFragments(recordsBeanList));
    }

    /**
     * 获取当前选中的vin
     */
    public String getVin(int position) {
        return fragmentList.get(position).getVin();
    }


    /**
     * @param records
     * @return 根据状态生产fragment
     */
    public List<BaseCarFragment> getFragments(List<RxCarListBean.RecordsBean> records) {
        for (int i = 0; i < records.size(); i++) {
            int nowType = MyUtils.carType(records.get(i).getOnlineStatus(), records.get(i).getChargeStatus(), records.get(i).getAlarmStatus());
            fragmentList.add(getCarFragment(nowType, records.get(i).getPn(), records.get(i).getVin()));
        }
        return fragmentList;
    }

    /**
     * 根据收到的mqtt消息刷新fragment
     */
    public void updateMqtt(final MqttVehicleBean mqttVehicleBean, final String vin, int carType, int carDetailType) {
        for (int i = 0; i < fragmentList.size(); i++) {
            if (fragmentList.get(i).getVin().equals(vin)) {

                fragmentList.get(i).mqttUpdate(mqttVehicleBean, carType, carDetailType);
            }
        }
    }


    /**
     * 根据状态返回对应的车辆界面
     */
    private BaseCarFragment getCarFragment(int statue, String plate, String vin) {
        CarStatuesFragment carStatuesFragment = CarStatuesFragment.newInstance(plate, vin);
        carStatuesFragment.setInterface(carFragmentInterface);
        return carStatuesFragment;
    }


    /**
     * 添加viewpager 导航圆点
     */
    public void addPoint(LinearLayout ln_point, int size) {

        imageViews = new ImageView[size];
        // 设置每个小圆点距离左边的间距
        for (int i = 0; i < size; i++) {
            ImageView imageView = new ImageView(context.getContext());
            // 设置每个小圆点的宽高
            //imageView.setLayoutParams(new LinearLayout.LayoutParams(10, 10));
            imageViews[i] = imageView;
            if (i == 0) {
                // 默认选中第一张图片
                imageViews[i].setBackgroundResource(R.mipmap.icon_point_choose);
                LinearLayout.LayoutParams margin = new LinearLayout.LayoutParams(chooseWidth, chooseHeight);
                margin.setMargins(distance, distance, distance, 0);
                ln_point.addView(imageViews[i], margin);
            } else {
                // 其他图片都设置未选中状态
                LinearLayout.LayoutParams margin = new LinearLayout.LayoutParams(
                        (int) context.getResources().getDimension(R.dimen.point),
                        (int) context.getResources().getDimension(R.dimen.point));
                margin.setMargins(distance, distance, distance, 0);
                imageViews[i].setBackgroundResource(R.mipmap.icon_point);
                ln_point.addView(imageViews[i], margin);
            }

        }
    }

    /**
     * viewpager切换时刷新导航和界面
     * 返回当前车辆的vin
     */
    public String pageChange(int position) {
        currentPosition = position;
        justInterface.ChangePlate(fragmentList.get(position).getPlate());
        // 遍历数组让当前选中图片下的小圆点设置颜色
        for (int i = 0; i < imageViews.length; i++) {

            if (position != i) {
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(point, point);
                params.setMargins(distance, distance, distance, 0);
                imageViews[i].setLayoutParams(params);
                imageViews[i].setBackgroundResource(R.mipmap.icon_point);
            } else {
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        chooseWidth, chooseHeight);
                params.setMargins(distance, distance, distance, 0);
                imageViews[position].setLayoutParams(params);
                imageViews[position].setBackgroundResource(R.mipmap.icon_point_choose);
            }
        }

        return fragmentList.get(position).getVin();
    }

    /**
     * @return
     */
    public boolean isUpdate(List<RxCarListBean.RecordsBean> records) {
        if (records.size() != fragmentList.size()) {
            return true;
        }
        for (RxCarListBean.RecordsBean r : records) {
            boolean has = false;
            for (BaseCarFragment b : fragmentList) {
                if (b.getVin().equals(r.getVin())) {
                    has = true;
                }
            }
            if (!has) {
                return true;
            }
        }
        return false;
    }

    /**
     * 返回当前选中的fragment
     *
     * @return
     */
    public BaseCarFragment getCurrentFragment() {
        return fragmentList.get(currentPosition);
    }
}
